// 发送ajax请求
import config from './config'
export default  (url, data={}, method='POST') => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: config.host + url,
      data,
      method,
      header: {
        cookie: wx.getStorageSync('cookies')?wx.getStorageSync('cookies') : ''
      },
      success: (res) => {
        console.log('请求成功',res)
        if ( res.cookies[0] ){
          wx.setStorageSync('cookies', res.cookies[0])
          // wx.getStorageSync('cookies')
        }
        resolve(res.data); // resolve修改promise的状态为成功状态resolved
      },
      fail: (err) => {
        console.log('请求失败: ', err);
        wx.showToast({
          title: '网络错误',
          icon:'none'
        })
        reject(err); // reject修改promise的状态为失败状态 rejected
      }
    })
  })
}
